package com.neusoft.lohas.modules.ivm.entity.merchant;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.modules.ivm.entity.product.ProductClassify;
import com.neusoft.lohas.modules.sys.entity.Dict;

@Entity
@Table(name = "ivm_time_slot")
@DynamicInsert @DynamicUpdate
public class TimeSlot extends IdEntity<TimeSlot>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ReceiptTime receiptTime; //主表信息
	private String timeText; //收货时间信息，文本
	private String startTime;
	private String endTime;
	private int usual; //平时
	private int weekend; //周末
	private int afterSixteen; //16 点后订单
	private Integer sort;
	
	private List<Dict> areaList = Lists.newArrayList(); // 区域列表
	
	public TimeSlot() {
		super();
	}

	public TimeSlot(String id) {
		this();
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="receiptTime_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message="所属收获信息不能为空")
	public ReceiptTime getReceiptTime() {
		return receiptTime;
	}

	public void setReceiptTime(ReceiptTime receiptTime) {
		this.receiptTime = receiptTime;
	}

	public String getTimeText() {
		return timeText;
	}

	public void setTimeText(String timeText) {
		this.timeText = timeText;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public int getUsual() {
		return usual;
	}

	public void setUsual(int usual) {
		this.usual = usual;
	}

	public int getWeekend() {
		return weekend;
	}

	public void setWeekend(int weekend) {
		this.weekend = weekend;
	}

	public int getAfterSixteen() {
		return afterSixteen;
	}

	public void setAfterSixteen(int afterSixteen) {
		this.afterSixteen = afterSixteen;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
	@JoinTable(name = "ivm_relationship_timeslot_area", joinColumns = { @JoinColumn(name = "time_id") }, inverseJoinColumns = { @JoinColumn(name = "dict_id") })
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("id") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@JsonIgnore
	public List<Dict> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<Dict> areaList) {
		this.areaList = areaList;
	}
	
	@Transient
	@JsonIgnore
	public List<String> getAreaIdList() {
		List<String> areaIdList = Lists.newArrayList();
		for (Dict area : areaList) {
			areaIdList.add(area.getId());
		}
		return areaIdList;
	}

	@Transient
	public void setAreaIdList(List<String> areaIdList) {
		areaList = Lists.newArrayList();
		if(areaIdList==null) return;
		for (String areaId : areaIdList) {
			Dict area = new Dict();
			area.setId(areaId);
			areaList.add(area);
		}
	}

}
